home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / nt / gr564s.zip / SRC / RCSGEN.C < prev    next >
C/C++ Source or Header  |  1992-09-05  |  17KB  |  658 lines

  1. /*
  2.  *                     RCS revision generation
  3.  */
  4.  
  5. /* Copyright (C) 1982, 1988, 1989 Walter Tichy
  6.    Copyright 1990, 1991, 1992 by Paul Eggert
  7.    Distributed under license by the Free Software Foundation, Inc.
  8.  
  9. This file is part of RCS.
  10.  
  11. RCS is free software; you can redistribute it and/or modify
  12. it under the terms of the GNU General Public License as published by
  13. the Free Software Foundation; either version 2, or (at your option)
  14. any later version.
  15.  
  16. RCS is distributed in the hope that it will be useful,
  17. but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19. GNU General Public License for more details.
  20.  
  21. You should have received a copy of the GNU General Public License
  22. along with RCS; see the file COPYING.  If not, write to
  23. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  24.  
  25. Report problems and direct all questions to:
  26.  
  27.     rcs-bugs@cs.purdue.edu
  28.  
  29. */
  30.  
  31.  
  32.  
  33. /* $Log: rcsgen.c,v $
  34.  * Revision 5.12  1992/07/28  16:12:44  eggert
  35.  * Statement macro names now end in _.
  36.  * Be consistent about pathnames vs filenames.
  37.  *
  38.  * Revision 5.11  1992/01/24  18:44:19  eggert
  39.  * Move put routines here from rcssyn.c.
  40.  * Add support for bad_creat0.
  41.  *
  42.  * Revision 5.10  1991/10/07  17:32:46  eggert
  43.  * Fix log bugs, e.g. ci -t/dev/null when has_mmap.
  44.  *
  45.  * Revision 5.9  1991/09/10  22:15:46  eggert
  46.  * Fix test for redirected stdin.
  47.  *
  48.  * Revision 5.8  1991/08/19  03:13:55  eggert
  49.  * Add piece tables.  Tune.
  50.  *
  51.  * Revision 5.7  1991/04/21  11:58:24  eggert
  52.  * Add MS-DOS support.
  53.  *
  54.  * Revision 5.6  1990/12/27  19:54:26  eggert
  55.  * Fix bug: rcs -t inserted \n, making RCS file grow.
  56.  *
  57.  * Revision 5.5  1990/12/04  05:18:45  eggert
  58.  * Use -I for prompts and -q for diagnostics.
  59.  *
  60.  * Revision 5.4  1990/11/01  05:03:47  eggert
  61.  * Add -I and new -t behavior.  Permit arbitrary data in logs.
  62.  *
  63.  * Revision 5.3  1990/09/21  06:12:43  hammer
  64.  * made putdesc() treat stdin the same whether or not it was from a terminal
  65.  * by making it recognize that a single '.' was then end of the
  66.  * description always
  67.  *
  68.  * Revision 5.2  1990/09/04  08:02:25  eggert
  69.  * Fix `co -p1.1 -ko' bug.  Standardize yes-or-no procedure.
  70.  *
  71.  * Revision 5.1  1990/08/29  07:14:01  eggert
  72.  * Clean old log messages too.
  73.  *
  74.  * Revision 5.0  1990/08/22  08:12:52  eggert
  75.  * Remove compile-time limits; use malloc instead.
  76.  * Ansify and Posixate.
  77.  *
  78.  * Revision 4.7  89/05/01  15:12:49  narten
  79.  * changed copyright header to reflect current distribution rules
  80.  * 
  81.  * Revision 4.6  88/08/28  14:59:10  eggert
  82.  * Shrink stdio code size; allow cc -R; remove lint; isatty() -> ttystdin()
  83.  * 
  84.  * Revision 4.5  87/12/18  11:43:25  narten
  85.  * additional lint cleanups, and a bug fix from the 4.3BSD version that
  86.  * keeps "ci" from sticking a '\377' into the description if you run it
  87.  * with a zero-length file as the description. (Guy Harris)
  88.  * 
  89.  * Revision 4.4  87/10/18  10:35:10  narten
  90.  * Updating version numbers. Changes relative to 1.1 actually relative to
  91.  * 4.2
  92.  * 
  93.  * Revision 1.3  87/09/24  13:59:51  narten
  94.  * Sources now pass through lint (if you ignore printf/sprintf/fprintf 
  95.  * warnings)
  96.  * 
  97.  * Revision 1.2  87/03/27  14:22:27  jenkins
  98.  * Port to suns
  99.  * 
  100.  * Revision 4.2  83/12/02  23:01:39  wft
  101.  * merged 4.1 and 3.3.1.1 (clearerr(stdin)).
  102.  * 
  103.  * Revision 4.1  83/05/10  16:03:33  wft
  104.  * Changed putamin() to abort if trying to reread redirected stdin.
  105.  * Fixed getdesc() to output a prompt on initial newline.
  106.  * 
  107.  * Revision 3.3.1.1  83/10/19  04:21:51  lepreau
  108.  * Added clearerr(stdin) for re-reading description from stdin.
  109.  * 
  110.  * Revision 3.3  82/11/28  21:36:49  wft
  111.  * 4.2 prerelease
  112.  * 
  113.  * Revision 3.3  82/11/28  21:36:49  wft
  114.  * Replaced ferror() followed by fclose() with ffclose().
  115.  * Putdesc() now suppresses the prompts if stdin
  116.  * is not a terminal. A pointer to the current log message is now
  117.  * inserted into the corresponding delta, rather than leaving it in a
  118.  * global variable.
  119.  *
  120.  * Revision 3.2  82/10/18  21:11:26  wft
  121.  * I added checks for write errors during editing, and improved
  122.  * the prompt on putdesc().
  123.  *
  124.  * Revision 3.1  82/10/13  15:55:09  wft
  125.  * corrected type of variables assigned to by getc (char --> int)
  126.  */
  127.  
  128.  
  129.  
  130.  
  131. #include "rcsbase.h"
  132.  
  133. libId(genId, "$Id: rcsgen.c,v 5.12 1992/07/28 16:12:44 eggert Exp $")
  134.  
  135. int interactiveflag;  /* Should we act as if stdin is a tty?  */
  136. struct buf curlogbuf;  /* buffer for current log message */
  137.  
  138. enum stringwork { enter, copy, edit, expand, edit_expand };
  139. static void scandeltatext P((struct hshentry*,enum stringwork,int));
  140.  
  141.  
  142.  
  143.  
  144.     char const *
  145. buildrevision(deltas, target, outfile, expandflag)
  146.     struct hshentries const *deltas;
  147.     struct hshentry *target;
  148.     FILE *outfile;
  149.     int expandflag;
  150. /* Function: Generates the revision given by target
  151.  * by retrieving all deltas given by parameter deltas and combining them.
  152.  * If outfile is set, the revision is output to it,
  153.  * otherwise written into a temporary file.
  154.  * Temporary files are allocated by maketemp().
  155.  * if expandflag is set, keyword expansion is performed.
  156.  * Return nil if outfile is set, the name of the temporary file otherwise.
  157.  *
  158.  * Algorithm: Copy initial revision unchanged.  Then edit all revisions but
  159.  * the last one into it, alternating input and output files (resultname and
  160.  * editname). The last revision is then edited in, performing simultaneous
  161.  * keyword substitution (this saves one extra pass).
  162.  * All this simplifies if only one revision needs to be generated,
  163.  * or no keyword expansion is necessary, or if output goes to stdout.
  164.  */
  165. {
  166.     if (deltas->first == target) {
  167.                 /* only latest revision to generate */
  168.         openfcopy(outfile);
  169.         scandeltatext(target, expandflag?expand:copy, true);
  170.         if (outfile)
  171.             return 0;
  172.         else {
  173.             Ozclose(&fcopy);
  174.             return resultname;
  175.         }
  176.         } else {
  177.                 /* several revisions to generate */
  178.         /* Get initial revision without keyword expansion.  */
  179.         scandeltatext(deltas->first, enter, false);
  180.         while ((deltas=deltas->rest)->rest) {
  181.                         /* do all deltas except last one */
  182.             scandeltatext(deltas->first, edit, false);
  183.                 }
  184.         if (expandflag || outfile) {
  185.                         /* first, get to beginning of file*/
  186.             finishedit((struct hshentry *)nil, outfile, false);
  187.                 }
  188.         scandeltatext(deltas->first, expandflag?edit_expand:edit, true);
  189.         finishedit(
  190.             expandflag ? deltas->first : (struct hshentry*)nil,
  191.             outfile, true
  192.         );
  193.         if (outfile)
  194.             return 0;
  195.         Ozclose(&fcopy);
  196.         return resultname;
  197.         }
  198. }
  199.  
  200.  
  201.  
  202.     static void
  203. scandeltatext(delta, func, needlog)
  204.     struct hshentry * delta;
  205.     enum stringwork func;
  206.     int needlog;
  207. /* Function: Scans delta text nodes up to and including the one given
  208.  * by delta. For the one given by delta, the log message is saved into
  209.  * delta->log if needlog is set; func specifies how to handle the text.
  210.  * Assumes the initial lexeme must be read in first.
  211.  * Does not advance nexttok after it is finished.
  212.  */
  213. {
  214.     struct hshentry const *nextdelta;
  215.     struct cbuf cb;
  216.  
  217.         for (;;) {
  218.         if (eoflex())
  219.             fatserror("can't find delta for revision %s", delta->num);
  220.                 nextlex();
  221.                 if (!(nextdelta=getnum())) {
  222.             fatserror("delta number corrupted");
  223.                 }
  224.         getkeystring(Klog);
  225.         if (needlog && delta==nextdelta) {
  226.             cb = savestring(&curlogbuf);
  227.             delta->log = cleanlogmsg(curlogbuf.string, cb.size);
  228.                 } else {readstring();
  229.                 }
  230.                 nextlex();
  231.         while (nexttok==ID && strcmp(NextString,Ktext)!=0)
  232.             ignorephrase();
  233.         getkeystring(Ktext);
  234.  
  235.         if (delta==nextdelta)
  236.             break;
  237.         readstring(); /* skip over it */
  238.  
  239.     }
  240.     switch (func) {
  241.         case enter: enterstring(); break;
  242.         case copy: copystring(); break;
  243.         case expand: xpandstring(delta); break;
  244.         case edit: editstring((struct hshentry *)nil); break;
  245.         case edit_expand: editstring(delta); break;
  246.     }
  247. }
  248.  
  249.     struct cbuf
  250. cleanlogmsg(m, s)
  251.     char *m;
  252.     size_t s;
  253. {
  254.     register char *t = m;
  255.     register char const *f = t;
  256.     struct cbuf r;
  257.     while (s) {
  258.         --s;
  259.         if ((*t++ = *f++) == '\n')
  260.         while (m < --t)
  261.             if (t[-1]!=' ' && t[-1]!='\t') {
  262.             *t++ = '\n';
  263.             break;
  264.             }
  265.     }
  266.     while (m < t  &&  (t[-1]==' ' || t[-1]=='\t' || t[-1]=='\n'))
  267.         --t;
  268.     r.string = m;
  269.     r.size = t - m;
  270.     return r;
  271. }
  272.  
  273.  
  274. int ttystdin()
  275. {
  276.     static int initialized;
  277.     if (!initialized) {
  278.         if (!interactiveflag)
  279.             interactiveflag = isatty(STDIN_FILENO);
  280.         initialized = true;
  281.     }
  282.     return interactiveflag;
  283. }
  284.  
  285.     int
  286. getcstdin()
  287. {
  288.     register FILE *in;
  289.     register int c;
  290.  
  291.     in = stdin;
  292.     if (feof(in) && ttystdin())
  293.         clearerr(in);
  294.     c = getc(in);
  295.     if (c < 0) {
  296.         testIerror(in);
  297.         if (feof(in) && ttystdin())
  298.             afputc('\n',stderr);
  299.     }
  300.     return c;
  301. }
  302.  
  303. #if has_prototypes
  304.     int
  305. yesorno(int default_answer, char const *question, ...)
  306. #else
  307.         /*VARARGS2*/ int
  308.     yesorno(default_answer, question, va_alist)
  309.         int default_answer; char const *question; va_dcl
  310. #endif
  311. {
  312.     va_list args;
  313.     register int c, r;
  314.     if (!quietflag && ttystdin()) {
  315.         oflush();
  316.         vararg_start(args, question);
  317.         fvfprintf(stderr, question, args);
  318.         va_end(args);
  319.         eflush();
  320.         r = c = getcstdin();
  321.         while (c!='\n' && !feof(stdin))
  322.             c = getcstdin();
  323.         if (r=='y' || r=='Y')
  324.             return true;
  325.         if (r=='n' || r=='N')
  326.             return false;
  327.     }
  328.     return default_answer;
  329. }
  330.  
  331.  
  332.     void
  333. putdesc(textflag, textfile)
  334.     int textflag;
  335.     char *textfile;
  336. /* Function: puts the descriptive text into file frewrite.
  337.  * if finptr && !textflag, the text is copied from the old description.
  338.  * Otherwise, if the textfile!=nil, the text is read from that
  339.  * file, or from stdin, if textfile==nil.
  340.  * A textfile with a leading '-' is treated as a string, not a pathname.
  341.  * If finptr, the old descriptive text is discarded.
  342.  * Always clears foutptr.
  343.  */
  344. {
  345.     static struct buf desc;
  346.     static struct cbuf desclean;
  347.  
  348.     register FILE *txt;
  349.     register int c;
  350.     register FILE * frew;
  351.     register char *p;
  352.     register size_t s;
  353.     char const *plim;
  354.  
  355.     frew = frewrite;
  356.     if (finptr && !textflag) {
  357.                 /* copy old description */
  358.         aprintf(frew, "\n\n%s%c", Kdesc, nextc);
  359.         foutptr = frewrite;
  360.         getdesc(false);
  361.         foutptr = 0;
  362.         } else {
  363.         foutptr = 0;
  364.                 /* get new description */
  365.         if (finptr) {
  366.                         /*skip old description*/
  367.             getdesc(false);
  368.                 }
  369.         aprintf(frew,"\n\n%s\n%c",Kdesc,SDELIM);
  370.         if (!textfile)
  371.             desclean = getsstdin(
  372.                 "t-", "description",
  373.                 "NOTE: This is NOT the log message!\n", &desc
  374.             );
  375.         else if (!desclean.string) {
  376.             if (*textfile == '-') {
  377.                 p = textfile + 1;
  378.                 s = strlen(p);
  379.             } else {
  380.                 if (!(txt = fopen(textfile, "r")))
  381.                     efaterror(textfile);
  382.                 bufalloc(&desc, 1);
  383.                 p = desc.string;
  384.                 plim = p + desc.size;
  385.                 for (;;) {
  386.                     if ((c=getc(txt)) < 0) {
  387.                         testIerror(txt);
  388.                         if (feof(txt))
  389.                             break;
  390.                     }
  391.                     if (plim <= p)
  392.                         p = bufenlarge(&desc, &plim);
  393.                     *p++ = c;
  394.                 }
  395.                 if (fclose(txt) != 0)
  396.                     Ierror();
  397.                 s = p - desc.string;
  398.                 p = desc.string;
  399.             }
  400.             desclean = cleanlogmsg(p, s);
  401.         }
  402.         putstring(frew, false, desclean, true);
  403.         aputc_('\n', frew)
  404.         }
  405. }
  406.  
  407.     struct cbuf
  408. getsstdin(option, name, note, buf)
  409.     char const *option, *name, *note;
  410.     struct buf *buf;
  411. {
  412.     register int c;
  413.     register char *p;
  414.     register size_t i;
  415.     register int tty = ttystdin();
  416.  
  417.     if (tty)
  418.         aprintf(stderr,
  419.         "enter %s, terminated with single '.' or end of file:\n%s>> ",
  420.         name, note
  421.         );
  422.     else if (feof(stdin))
  423.         faterror("can't reread redirected stdin for %s; use -%s<%s>",
  424.         name, option, name
  425.         );
  426.     
  427.     for (
  428.        i = 0,  p = 0;
  429.        c = getcstdin(),  !feof(stdin);
  430.        bufrealloc(buf, i+1),  p = buf->string,  p[i++] = c
  431.     )
  432.         if (c == '\n')
  433.             if (i  &&  p[i-1]=='.'  &&  (i==1 || p[i-2]=='\n')) {
  434.                 /* Remove trailing '.'.  */
  435.                 --i;
  436.                 break;
  437.             } else if (tty)
  438.                 aputs(">> ", stderr);
  439.     return cleanlogmsg(p, i);
  440. }
  441.  
  442.  
  443.     void
  444. putadmin()
  445. /* Output the admin node.  */
  446. {
  447.     register FILE *fout;
  448.     struct assoc const *curassoc;
  449.     struct lock const *curlock;
  450.     struct access const *curaccess;
  451.  
  452.     if (!(fout = frewrite)) {
  453. #        if bad_creat0
  454.             ORCSclose();
  455.             fout = fopen(makedirtemp(0), FOPEN_W);
  456. #        else
  457.             int fo = fdlock;
  458.             fdlock = -1;
  459.             fout = fdopen(fo, FOPEN_W);
  460. #        endif
  461.  
  462.         if (!(frewrite = fout))
  463.             efaterror(RCSname);
  464.     }
  465.  
  466.     aprintf(fout, "%s\t%s;\n", Khead, Head?Head->num:"");
  467.     if (Dbranch && VERSION(4)<=RCSversion)
  468.         aprintf(fout, "%s\t%s;\n", Kbranch, Dbranch);
  469.  
  470.     aputs(Kaccess, fout);
  471.     curaccess = AccessList;
  472.     while (curaccess) {
  473.            aprintf(fout, "\n\t%s", curaccess->login);
  474.            curaccess = curaccess->nextaccess;
  475.     }
  476.     aprintf(fout, ";\n%s", Ksymbols);
  477.     curassoc = Symbols;
  478.     while (curassoc) {
  479.            aprintf(fout, "\n\t%s:%s", curassoc->symbol, curassoc->num);
  480.            curassoc = curassoc->nextassoc;
  481.     }
  482.     aprintf(fout, ";\n%s", Klocks);
  483.     curlock = Locks;
  484.     while (curlock) {
  485.            aprintf(fout, "\n\t%s:%s", curlock->login, curlock->delta->num);
  486.            curlock = curlock->nextlock;
  487.     }
  488.     if (StrictLocks) aprintf(fout, "; %s", Kstrict);
  489.     aprintf(fout, ";\n");
  490.     if (Comment.size) {
  491.         aprintf(fout, "%s\t", Kcomment);
  492.         putstring(fout, true, Comment, false);
  493.         aprintf(fout, ";\n");
  494.     }
  495.     if (Expand != KEYVAL_EXPAND)
  496.         aprintf(fout, "%s\t%c%s%c;\n",
  497.             Kexpand, SDELIM, expand_names[Expand], SDELIM
  498.         );
  499.     awrite(Ignored.string, Ignored.size, fout);
  500.     aputc_('\n', fout)
  501. }
  502.  
  503.  
  504.     static void
  505. putdelta(node, fout)
  506.     register struct hshentry const *node;
  507.     register FILE * fout;
  508. /* Output the delta NODE to FOUT.  */
  509. {
  510.     struct branchhead const *nextbranch;
  511.  
  512.     if (!node) return;
  513.  
  514.     aprintf(fout, "\n%s\n%s\t%s;\t%s %s;\t%s %s;\nbranches",
  515.         node->num,
  516.         Kdate, node->date,
  517.         Kauthor, node->author,
  518.         Kstate, node->state?node->state:""
  519.     );
  520.     nextbranch = node->branches;
  521.     while (nextbranch) {
  522.            aprintf(fout, "\n\t%s", nextbranch->hsh->num);
  523.            nextbranch = nextbranch->nextbranch;
  524.     }
  525.  
  526.     aprintf(fout, ";\n%s\t%s;\n", Knext, node->next?node->next->num:"");
  527.     awrite(node->ig.string, node->ig.size, fout);
  528. }
  529.  
  530.  
  531.     void
  532. puttree(root, fout)
  533.     struct hshentry const *root;
  534.     register FILE *fout;
  535. /* Output the delta tree with base ROOT in preorder to FOUT.  */
  536. {
  537.     struct branchhead const *nextbranch;
  538.  
  539.     if (!root) return;
  540.  
  541.     if (root->selector)
  542.         putdelta(root, fout);
  543.  
  544.     puttree(root->next, fout);
  545.  
  546.     nextbranch = root->branches;
  547.     while (nextbranch) {
  548.          puttree(nextbranch->hsh, fout);
  549.          nextbranch = nextbranch->nextbranch;
  550.     }
  551. }
  552.  
  553.  
  554.     int
  555. putdtext(num, log, srcname, fout, diffmt)
  556.     char const *num, *srcname;
  557.     struct cbuf log;
  558.     FILE *fout;
  559.     int diffmt;
  560. /*
  561.  * Output a deltatext node with delta number NUM, log message LOG and
  562.  * text SRCNAME to FOUT.
  563.  * Double up all SDELIMs in both the log and the text.
  564.  * Make sure the log message ends in \n.
  565.  * Return false on error.
  566.  * If DIFFMT, also check that the text is valid diff -n output.
  567.  */
  568. {
  569.     RILE *fin;
  570.     if (!(fin = Iopen(srcname, "r", (struct stat*)0))) {
  571.         eerror(srcname);
  572.         return false;
  573.     }
  574.     putdftext(num, log, fin, fout, diffmt);
  575.     Ifclose(fin);
  576.     return true;
  577. }
  578.  
  579.     void
  580. putstring(out, delim, s, log)
  581.     register FILE *out;
  582.     struct cbuf s;
  583.     int delim, log;
  584. /*
  585.  * Output to OUT one SDELIM if DELIM, then the string S with SDELIMs doubled.
  586.  * If LOG is set then S is a log string; append a newline if S is nonempty.
  587.  */
  588. {
  589.     register char const *sp;
  590.     register size_t ss;
  591.  
  592.     if (delim)
  593.         aputc_(SDELIM, out)
  594.     sp = s.string;
  595.     for (ss = s.size;  ss;  --ss) {
  596.         if (*sp == SDELIM)
  597.             aputc_(SDELIM, out)
  598.         aputc_(*sp++, out)
  599.     }
  600.     if (s.size && log)
  601.         aputc_('\n', out)
  602.     aputc_(SDELIM, out)
  603. }
  604.  
  605.     void
  606. putdftext(num, log, finfile, foutfile, diffmt)
  607.     char const *num;
  608.     struct cbuf log;
  609.     RILE *finfile;
  610.     FILE *foutfile;
  611.     int diffmt;
  612. /* like putdtext(), except the source file is already open */
  613. {
  614.     declarecache;
  615.     register FILE *fout;
  616.     register int c;
  617.     register RILE *fin;
  618.     int ed;
  619.     struct diffcmd dc;
  620.  
  621.     fout = foutfile;
  622.     aprintf(fout, DELNUMFORM, num, Klog);
  623.  
  624.     /* put log */
  625.     putstring(fout, true, log, true);
  626.  
  627.     /* put text */
  628.     aprintf(fout, "\n%s\n%c", Ktext, SDELIM);
  629.  
  630.     fin = finfile;
  631.     setupcache(fin);
  632.     if (!diffmt) {
  633.         /* Copy the file */
  634.         cache(fin);
  635.         for (;;) {
  636.         cachegeteof_(c, break;)
  637.         if (c==SDELIM) aputc_(SDELIM, fout) /*double up SDELIM*/
  638.         aputc_(c, fout)
  639.         }
  640.     } else {
  641.         initdiffcmd(&dc);
  642.         while (0  <=  (ed = getdiffcmd(fin, false, fout, &dc)))
  643.         if (ed) {
  644.             cache(fin);
  645.             while (dc.nlines--)
  646.             do {
  647.                 cachegeteof_(c, { if (!dc.nlines) goto OK_EOF; unexpected_EOF(); })
  648.                 if (c == SDELIM)
  649.                 aputc_(SDELIM, fout)
  650.                 aputc_(c, fout)
  651.             } while (c != '\n');
  652.             uncache(fin);
  653.         }
  654.     }
  655.     OK_EOF:
  656.     aprintf(fout, "%c\n", SDELIM);
  657. }
  658.